package com.smithlibraties.util.annotation.register.render;

import com.smithlibraties.SmithLibraries;
import com.smithlibraties.util.annotation.AnnotationClassGetter;
import com.smithlibraties.util.annotation.register.Register;
import com.smithlibraties.util.annotation.register.RegisterClass;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.item.Item;
import net.minecraftforge.client.IItemRenderer;
import net.minecraftforge.client.MinecraftForgeClient;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@SideOnly(Side.CLIENT)
public class RegisterItemRender {
    List<List<RegisterRenderAttribute>> list = new ArrayList<>();//每个类为一个List，然后吧每个类的List集中到这个List

    @SideOnly(Side.CLIENT)
    public RegisterItemRender() {
        List<Class> itemBlockCollections = AnnotationClassGetter.getAllClassByInterface(SmithLibraries.getAllClasses(),RegisterClass.class);//获取实现RegisterClass注释的类的List
        for (Class clazz : itemBlockCollections) {//开始集中继承IRegister接口的的类的属性的List
            list.add(RegisterItemRender.getAnnotationList(clazz));
        }
        for (List<RegisterRenderAttribute> fieldList : list) {
            for (RegisterRenderAttribute renderField : fieldList) {
                if (renderField.getTextureName() != null) {
                    try {
                        RegisterItemRender.registerTextures(renderField.getModid(), renderField.getTextureName(), renderField.getField().get(null));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        if (IItemRenderer.class.isAssignableFrom(renderField.getClazz())) {
                            RegisterItemRender.registerRender(renderField.getClazz(), renderField.getField().get(null));
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @SideOnly(Side.CLIENT)
    private static void registerTextures(String modid, String textureName, Object object) {
        ((Item) object).setTextureName(modid + ":" + textureName);
    }

    @SideOnly(Side.CLIENT)
    private static void registerRender(Class clazz, Object object) {
        try {
            MinecraftForgeClient.registerItemRenderer((Item) object, (IItemRenderer) clazz.newInstance());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @SideOnly(Side.CLIENT)
    private static List<RegisterRenderAttribute> getAnnotationList(Class clazz) {
        List<RegisterRenderAttribute> list1 = new ArrayList<>();
        for (Field f : clazz.getFields()) {
            f.setAccessible(true);
            if (f.isAnnotationPresent(Register.TextureName.class)) {
                Register.TextureName textureRegister = f.getAnnotation(Register.TextureName.class);
                list1.add(new RegisterRenderAttribute(f, textureRegister.modid(), textureRegister.value()));
            }
            if (f.isAnnotationPresent(Register.ItemRender.class)) {
                Register.ItemRender renderRegister = f.getAnnotation(Register.ItemRender.class);
                if (IItemRenderer.class.isAssignableFrom(renderRegister.clazz())) {
                    list1.add(new RegisterRenderAttribute(f, renderRegister.clazz()));
                }
            }
        }
        return list1;
    }
}
